<template>
  <div class="music-container">
    <div class="common-layout">
      <el-container>
        <!-- <el-header class="border"></el-header> -->
        <el-container>
          <el-aside class="border" width="280px">
            <!-- 侧边导航栏 -->
            <div class="sidebar">
              <div class="logo-container">
                <h1>AiMusic</h1>
              </div>

              <nav class="main-nav">
                <el-menu default-active="1" class="el-menu-vertical-demo" @select="handleNavSelect">
                  <el-menu-item index="1">
                    <el-icon><HomeFilled /></el-icon><span>首页</span>
                  </el-menu-item>
                  <el-menu-item index="2">
                    <el-icon><Music /></el-icon><span>Ai 音乐</span>
                  </el-menu-item>
                  <el-menu-item index="3">
                    <el-icon><VideoPlay /></el-icon><span>Ai 创作</span>
                  </el-menu-item>
                  <el-menu-item index="4">
                    <el-icon><Collection /></el-icon><span>充值</span>
                  </el-menu-item>
                  <el-menu-item index="5">
                    <el-icon><Download /></el-icon><span>订单</span>
                  </el-menu-item>
                </el-menu>
              </nav>

              <div class="user-playlists">
                <h3>我的歌单</h3>
                <div class="playlist-item" v-for="(playlist, index) in playlists" :key="index">
                  <span>{{ playlist.name }}</span>
                </div>
              </div>
            </div>

          </el-aside>
          <el-container>
            <el-main class="border1">
              <!-- 主内容区域 - 歌曲选择 -->
              <div class="main-content-wrapper">
                
                <div class="main-content">
                  <!-- 搜索栏 -->
                  <div class="search-bar">
                    <el-row :gutter="10">
                      <el-col :span="16">
                        <el-input v-model="searchQuery" placeholder="搜索音乐、歌手、专辑" :prefix-icon="Search" clearable
                          :style="inputStyle" @keyup.enter="handleSearch" />
                      </el-col>
                      <el-col :span="2">
                        <el-button type="primary" :style="buttonStyle" @click="handleSearch">搜索</el-button>
                      </el-col>
                      <el-col offset="6" :span="6">
                      </el-col>
                    </el-row>
                    <el-dropdown trigger="click" placement="bottom">
                          <div class="user-info-container">
                            <el-image
                              src="https://demoo-1app.oss-cn-beijing.aliyuncs.com/2025-10-28-17-46-11OIP-C (2).jpg"
                              alt="用户头像" class="user-avatar" />
                            <span class="user-name">用户名</span>
                          </div>
                          <template #dropdown>
                            <el-dropdown-menu>
                              <el-dropdown-item>个人中心</el-dropdown-item>
                              <el-dropdown-item>账号设置</el-dropdown-item>
                              <el-dropdown-item divided>退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                          </template>
                        </el-dropdown>
                  </div>
                  <!-- <hr> -->
                  <RouterView />
                </div>
              </div>

            </el-main>
            <el-footer class="border-top">

            </el-footer>
          </el-container>
        </el-container>
      </el-container>
    </div>

    <!-- 底部音乐播放器 -->
    <div class="music-player" v-if="currentSong">
      <div class="player-left">
        <div class="song-cover-small">
          <img :src="currentSong" alt="{{ currentSong.name }}">
        </div>
        <div class="song-info">
          <h4>{{ currentSong.name }}</h4>
          <p>{{ currentSong.artist }}</p>
        </div>
        <div class="player-controls-left">
          <el-button type="text" :icon="LikeFilled"></el-button>
          <el-button type="text" :icon="Collection"></el-button>
        </div>
      </div>

      <div class="player-center">
        <div class="play-buttons">
          <el-button type="text" :icon="Shuffle"></el-button>
          <el-button type="text" :icon="StepBackward"></el-button>
          <el-button :icon="isPlaying ? Pause : Play" size="large" @click="togglePlay"
            :style="playButtonStyle"></el-button>
          <el-button type="text" :icon="StepForward"></el-button>
          <el-button type="text" :icon="RefreshRight"></el-button>
        </div>
        <div class="progress-bar">
          <span class="time-current">{{ formatTime(currentTime) }}</span>
          <el-slider v-model="progress" :format-tooltip="formatTooltip" @change="onProgressChange"></el-slider>
          <span class="time-total">{{ formatTime(totalTime) }}</span>
        </div>
      </div>

      <div class="player-right">
        <el-button type="text" :icon="List"></el-button>
        <el-button type="text" :icon="Volume"></el-button>
        <el-slider v-model="volume" :format-tooltip="formatTooltip" class="volume-slider"
          @change="onVolumeChange"></el-slider>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import api from '@/utils/api.js'
import axios from 'axios'
import router from '@/router'
// import {
//   HomeFilled, Music, VideoPlay, Radio, Collection, Download, Search,
//   Play, Pause, StepBackward, StepForward, Shuffle, RefreshRight,
//   LikeFilled, List, Volume, MoreFilled
// } from 'element-plus/icons-vue'

// 导航相关
const activeNav = ref()
const searchQuery = ref('')

// 播放状态
const isPlaying = ref(false)
const currentSong = ref("https://picsum.photos/seed/album1/300/300")
const currentTime = ref(0)
const totalTime = ref(237)
const progress = ref(0)
const volume = ref(70)
const token = ref(localStorage.getItem("token"))

console.log(token.value);

const test1 = async () => {

  let headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With': 'XMLHttpRequest',
  }

  try {
    const formData = new URLSearchParams();
    formData.append('token', localStorage.getItem("token"));
    axios.post(api.test, formData, {
      headers: headers
    })
      .then(function (response) {
        console.log(response.data);
        if (response.data.code == 200) {
          console.log(response.data.data.userName);

        } else {
          ElMessage.error(response.data.data)
        }
      })
      .catch(function (error) {
        console.log(error);
      });
  } catch (error) {
  } finally {
  }

}

test1()


// 示例数据 - 我的歌单
const playlists = reactive([
  { id: 1, name: '我喜欢的音乐' },
  { id: 2, name: '工作必备' },
  { id: 3, name: '轻松午后' },
  { id: 4, name: '跑步健身' }
])


// 样式计算属性
const inputStyle = computed(() => ({
  borderRadius: '20px',
  borderColor: '#dcdfe6',
  '&:hover': {
    borderColor: '#c0c4cc'
  },
  '&.is-focus': {
    borderColor: '#8294c4',
    boxShadow: '0 0 0 2px rgba(130, 148, 196, 0.2)'
  }
}))

const playButtonStyle = computed(() => ({
  backgroundColor: '#8294c4',
  borderColor: '#8294c4',
  '&:hover': {
    backgroundColor: '#6b82b6',
    borderColor: '#6b82b6',
    transform: 'scale(1.05)'
  },
  transition: 'all 0.3s ease'
}))

const headerCellStyle = computed(() => ({
  backgroundColor: '#f5f7fa',
  fontSize: '14px',
  color: '#606266'
}))

// 导航选择处理
function handleNavSelect(index) {
  activeNav.value = index
  // 这里可以根据选择的导航项加载不同的内容
  router.push(getNavName(index))
  ElMessage.info(`已切换到${getNavName(index)}`)
}

// 获取导航名称
function getNavName(index) {
  const names = ['/HomeMusic', '/AiMusic', '/AiCreate', '/TopUp','/Order']
  return names[index - 1] || '未知页面'
}

// 搜索处理
function handleSearch() {
  if (searchQuery.value.trim()) {
    ElMessage.info(`搜索: ${searchQuery.value}`)
    // 实际应用中这里应该调用搜索API
  }
}

// 查看更多
function viewMore() {
  ElMessage.info('查看更多推荐内容')
}

// 播放歌曲
function playSong(song) {
  currentSong.value = {
    ...song,
    cover: `https://picsum.photos/seed/${song.id}/300/300`
  }
  isPlaying.value = true
  ElMessage.success(`正在播放: ${song.name} - ${song.artist}`)
}

// 切换播放状态
function togglePlay() {
  isPlaying.value = !isPlaying.value
  if (isPlaying.value && !currentSong.value) {
    // 如果当前没有播放的歌曲，默认播放第一首热门歌曲
    playSong(hotSongs[0])
  }
}

// 格式化时间
function formatTime(seconds) {
  const mins = Math.floor(seconds / 60)
  const secs = Math.floor(seconds % 60)
  return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
}

// 格式化进度条提示
function formatTooltip(value) {
  return ''
}

// 进度条变化处理
function onProgressChange(value) {
  currentTime.value = Math.floor((value / 100) * totalTime.value)
}

// 音量变化处理
function onVolumeChange(value) {
  // 实际应用中这里应该调节音量
  ElMessage.info(`音量已调整为: ${value}%`)
}
</script>

<style scoped>
/* 全局样式重置和变量 */
:root {
  --primary-color: #8294c4;
  --primary-hover: #6b82b6;
  --sidebar-bg: #1a1a1a;
  --main-bg: #f5f7fa;
  --card-bg: #ffffff;
  --text-primary: #333333;
  --text-secondary: #606266;
  --text-muted: #999999;
  --border-color: #e4e7ed;
  --shadow-light: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* 整体容器 */
.music-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  background-color: var(--main-bg);
  overflow: hidden;
  position: relative;
  background-color: #f0f0f0;
  border-radius: 8px;
  margin: 2px;
}

/* 主内容区域样式 */


/* 底部音乐播放器样式 */
.music-player {
  width: 100%;
  height: 90px;
  background-color: rgba(175, 175, 12, 0.98);
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}

/* 侧边栏样式 */
.sidebar {
    /* 圆角属性 */
  border-radius: 8px;
  background-color: #f5f7fa;
  flex: 1;
  margin: 20px;
  height: calc(100vh - 140px);
  /* 减去顶部导航和底部播放器的高度 */
  overflow-y: auto;
  /* 允许上下滑动 */
  overflow-x: hidden;
  /* 防止出现水平滚动条 */
  width: 240px;
  /* background-color: var(--sidebar-bg); */
  color: #000000;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  /* 允许上下滑动 */
  overflow-x: hidden;
  /* 禁止左右滑动 */
  transition: all 0.3s ease;
}



.logo-container {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #2a2a2a;

}

.logo-container h1 {
  color: var(--primary-color);
  font-size: 24px;
  margin: 0;
  font-weight: 600;
  letter-spacing: 1px;
}

.main-nav {
  padding: 20px 0;
}

.el-menu-vertical-demo {
  background-color: transparent;
  border-right: none;
}

.el-menu-item {
  color: #444343;
  height: 50px;
  line-height: 50px;
  background-color: transparent !important;
  transition: all 0.3s ease;
}

.el-menu-item:hover {
  color: var(--primary-color);
  background-color: rgba(130, 148, 196, 0.05) !important;
}

.el-menu-item.is-active {
  color: var(--primary-color);
  background-color: rgba(130, 148, 196, 0.1) !important;
}

.user-playlists {
  margin-top: auto;
  padding: 20px;
  border-top: 1px solid #2a2a2a;
}

.user-playlists h3 {
  color: #fff;
  font-size: 14px;
  margin-bottom: 15px;
  font-weight: 500;
}

.playlist-item {
  padding: 8px 0;
  font-size: 13px;
  color: #383838;
  cursor: pointer;
  transition: color 0.3s ease, transform 0.2s ease;
}

.playlist-item:hover {
  color: var(--primary-color);
  transform: translateX(5px);
}

/* 主内容区域样式 */
.main-content {
  flex: 1;
  padding: 30px;
  overflow-y: hidden;
}
.main-content {
  flex: 1;
  padding: 20px;
  background-color: #ffffff;
  height: calc(100vh - 140px);
  border-radius: 8px;
  /* 减去顶部导航和底部播放器的高度 */
  overflow-y: hidden;
  /* 允许上下滑动 */
  overflow-x: hidden;
  /* 防止出现水平滚动条 */
}

/* 搜索栏样式 */
.search-bar {
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
}

.search-bar .el-input {
  width: 600px;
  max-width: 100%;
}

/* 区域头部样式 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.section-header h2 {
  color: var(--text-primary);
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  position: relative;
}

.section-header h2::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -8px;
  width: 40px;
  height: 3px;
  background-color: var(--primary-color);
  border-radius: 1.5px;
}

/* 专辑网格样式 */
.album-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 30px;
  margin-bottom: 60px;
}

.album-card {
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 15px;
  box-shadow: var(--shadow-light);
}

.album-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.album-cover {
  position: relative;
  margin-bottom: 15px;
  overflow: hidden;
  border-radius: 4px;
}

.album-cover img {
  width: 100%;
  height: auto;
  display: block;
  transition: transform 0.5s ease;
}

.album-card:hover .album-cover img {
  transform: scale(1.05);
}

.play-btn {
  position: absolute;
  bottom: 10px;
  right: 10px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: var(--primary-color);
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  opacity: 0;
  transition: all 0.3s ease;
  transform: translateX(10px);
}

.album-card:hover .play-btn {
  opacity: 1;
  transform: translateX(0);
}

.play-btn:hover {
  background-color: var(--primary-hover);
  transform: scale(1.1);
}

.album-card h3 {
  font-size: 14px;
  color: var(--text-primary);
  margin: 8px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}

.album-card p {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 热门歌曲表格样式 */
.hot-songs-section {
  background-color: var(--card-bg);
  border-radius: 8px;
  padding: 25px;
  box-shadow: var(--shadow-light);
}

.song-info {
  display: flex;
  align-items: center;
}

.song-name {
  font-size: 14px;
  color: var(--text-primary);
}

.mv-icon {
  margin-left: 8px;
  color: var(--primary-color);
  font-size: 14px;
}

/* 底部音乐播放器样式 */
.music-player {
  width: 100%;
  height: 90px;
  background-color: rgba(255, 255, 255, 0.98);
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);
}

.player-left {
  display: flex;
  align-items: center;
  width: 300px;
}

.song-cover-small {
  width: 60px;
  height: 60px;
  margin-right: 15px;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.song-cover-small img {
  width: 100%;
  height: 100%;
  transition: transform 0.5s ease;
}

.player-left:hover .song-cover-small img {
  transform: scale(1.08);
}

.song-info h4 {
  font-size: 14px;
  color: var(--text-primary);
  margin: 0 0 5px 0;
  font-weight: 500;
}

.song-info p {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0;
}

.player-controls-left {
  margin-left: 20px;
}

.player-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.play-buttons {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.play-buttons .el-button {
  margin: 0 10px;
  transition: all 0.3s ease;
}

.play-buttons .el-button:hover {
  transform: scale(1.1);
  color: var(--primary-color);
}

.progress-bar {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 800px;
}

.time-current,
.time-total {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 10px;
  min-width: 40px;
  text-align: center;
}

.progress-bar .el-slider {
  flex: 1;
}

.player-right {
  display: flex;
  align-items: center;
  width: 300px;
  justify-content: flex-end;
}

.player-right .el-button {
  margin: 0 10px;
  transition: all 0.3s ease;
}

.player-right .el-button:hover {
  transform: scale(1.1);
  color: var(--primary-color);
}

.volume-slider {
  width: 100px;
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 25px;
  }
}

@media (max-width: 1024px) {
  .sidebar {
    width: 200px;
  }

  .search-bar .el-input {
    width: 100%;
  }

  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 20px;
  }

  .player-left,
  .player-right {
    width: 250px;
  }
}

@media (max-width: 768px) {
  .sidebar {
    width: 60px;
  }

  .logo-container h1 {
    font-size: 16px;
  }

  .el-menu-item span {
    display: none;
  }

  .user-playlists {
    display: none;
  }

  .main-content {
    padding: 20px 15px;
  }

  .section-header h2 {
    font-size: 20px;
  }

  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 15px;
  }

  .player-left {
    width: 180px;
  }

  .song-info h4,
  .song-info p {
    font-size: 12px;
  }

  .player-right {
    width: 120px;
  }

  .volume-slider {
    display: none;
  }

  .play-buttons .el-button {
    margin: 0 8px;
  }
}

@media (max-width: 480px) {
  .album-grid {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 10px;
  }

  .album-card {
    padding: 10px;
  }

  .music-player {
    padding: 0 10px;
    height: 80px;
  }

  .player-left {
    width: 150px;
  }

  .song-cover-small {
    width: 50px;
    height: 50px;
  }
}

/* 滚动条样式优化 */
.sidebar::-webkit-scrollbar,
.main-content::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track,
.main-content::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.main-content::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover,
.main-content::-webkit-scrollbar-thumb:hover {
  background: rgba(130, 148, 196, 0.3);
}

/* `.border1 {
  height: 100%
}` */
.border-top {
  width: 2000px;
}

/* ... 其他样式保持不变 ... */
.user-info-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-info-container:hover {
  width: 100%;
  background-color: rgba(130, 148, 196, 0.1);
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 8px;
  border: 2px solid #f0f0f0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.user-info-container:hover .user-avatar {
  transform: scale(1.05);
  border-color: var(--primary-color);
}

.user-name {
  font-size: 14px;
  color: var(--text-primary);
  font-weight: 500;
}
</style>